package gold.digger;

import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC20 {

    public boolean isValid(String s) {
        if (null == s || s.length() <= 0) {
            return true;
        }

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            } else {
                if (stack.size() == 0) {
                    return false;
                }
                char topCh = stack.pop();
                if ((topCh == '(' && ch == ')') || (topCh == '[' && ch == ']') || (topCh == '{' && ch == '}')) {
                    continue;
                }
                return false;
            }
        }

        return stack.size() == 0;
    }

    public static void main(String[] args) {
        LC20 an = new LC20();
        System.out.println(an.isValid("]"));
    }
}
